package com.juli.aimanager.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.juli.aimanager.dao.BaseDao;
import com.juli.aimanager.system.dto.SysUserDO;
import com.juli.aimanager.system.dto.UserRoleDO;
import com.juli.aimanager.system.service.UserService;

@Service("userService")
public class UserServiceImp implements UserService {
	@Autowired
	public BaseDao baseDao;
	private static final Logger logger = LoggerFactory.getLogger(UserService.class);

	public SysUserDO get(Long id) throws Exception {
		String sql = "select role_id from sys_user_role where user_id="+id;
		String sysUserSql = "select 'user_id', 'username', 'password', 'email', 'mobile', 'status', 'user_id_create', 'gmt_create', 'gmt_modified', 'name' from sys_user where user_id ="+id;
		List<Long> roleIds = baseDao.queryListRoleId(sql);
		SysUserDO user = (SysUserDO) baseDao.findOneObject(sql, SysUserDO.class);
		user.setroleIds(roleIds);
		return user;
	}

	public List<SysUserDO> list(Map<String, Object> map) throws Exception {
		String sql = "select * from sys_user where 1=1 ";
		sql += " limit "+map.get("offset")+","+map.get("limit");
		return baseDao.findAllObject(sql, SysUserDO.class);
	}

	public int count(Map<String, Object> map) throws Exception {
		String sql = "select count(*) from sys_user where 1=1 ";
		sql += " limit "+map.get("offset")+","+map.get("limit");
		return baseDao.queryForInt(sql);
	}

	@Transactional
	public int save(SysUserDO user) throws Exception {
//		int r = 0;
//		userMapper.save(user);
//		Long userId = user.getUserId();
//		List<Long> roles = user.getroleIds();
//		userRoleMapper.removeByUserId(userId);
//		for (Long roleId : roles) {
//			UserRoleDO ur = new UserRoleDO();
//			ur.setUserId(userId);
//			ur.setRoleId(roleId);
//			r = userRoleMapper.save(ur);
//		}
//		return r;
		return 0;
	}

	public int update(SysUserDO user) throws Exception {
//		int r = 0;
//		userMapper.update(user);
//		Long userId = user.getUserId();
//		List<Long> roles = user.getroleIds();
//		userRoleMapper.removeByUserId(userId);
//		for (Long roleId : roles) {
//			UserRoleDO ur = new UserRoleDO();
//			ur.setUserId(userId);
//			ur.setRoleId(roleId);
//			r = userRoleMapper.save(ur);
//		}
//		return r;
		return 0;
	}

	public int remove(Long userId) throws Exception {
//		userRoleMapper.removeByUserId(userId);
//		return userMapper.remove(userId);
		return 0;
	}

	public boolean exit(Map<String, Object> params) throws Exception {
//		boolean exit;
//		exit = userMapper.list(params).size() > 0;
//		return exit;
		return false;
	}

	public Set<String> listRoles(Long userId) throws Exception {
		return null;
	}

	public int resetPwd(SysUserDO user) {
//		int r = userMapper.update(user);
//		return r;
		return 0;
	}

	@Transactional
	public int batchremove(List<Long> userIds) throws Exception {
//		int count = userMapper.batchRemove(userIds);
//		userRoleMapper.batchRemoveByUserId(userIds);
//		return count;
		return 0;
	}

	public Map getUserList(Map<String, String> paramsMap) throws Exception {
		String sql = "select * from sys_user where 1=1 ";
		return baseDao.queryGridListMySql(sql, paramsMap);
	}

}
